package org.fly.jingwei.util;

import org.fly.jingwei.entity.UserEntity;
import org.fly.jingwei.entity.UserGroupAttributeEntity;
import org.fly.jingwei.exception.JingWeiSysException;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * HTTP servlet工具类
 *
 * @author leo.jie (weixiao.me@aliyun.com)
 * @version 1.0
 * @organization bigdata
 * @website https://www.jielongping.com
 * @date 2020/5/31 2:16 下午
 * @since 1.0
 */
public class HttpServletUtil {
    public static final String LOGIN_SESSION_KEY = "login_jingwei";
    public static final String LOGIN_USER_INFO_SESSION_KEY = "user_info_jingwei";
    public static final Map<String, HttpSession> ALL_SESSION = new HashMap<>();

    private static HttpServletRequest currentRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            throw new JingWeiSysException("系统未知异常");
        }
        return attributes.getRequest();
    }

    public static UserEntity getCurrentLoginUser() {
        HttpServletRequest request = currentRequest();
        Object value = request.getSession().getAttribute(LOGIN_SESSION_KEY);
        if (null != value) {
            return (UserEntity) value;
        }
        return null;
    }

    public static UserGroupAttributeEntity getUserGroupAttributeEntity(){
        HttpServletRequest request = currentRequest();
        Object value = request.getSession().getAttribute(LOGIN_USER_INFO_SESSION_KEY);
        if (null != value) {
            return (UserGroupAttributeEntity) value;
        }
        return null;
    }

    public static void saveUserInfoSession(UserEntity user, UserGroupAttributeEntity attribute) {
        //使用不同浏览器重复登录\清除缓冲再登录\关闭浏览器后再打开可能造成存在两个username属性相同的session
        //如果之前该用户session已存在 则先移除以前的session
        List<String> invalidSession = new ArrayList<>();
        Set<String> set = ALL_SESSION.keySet();
        for (String sessionId : set) {
            HttpSession session = ALL_SESSION.get(sessionId);
            if (null != session.getAttribute(LOGIN_SESSION_KEY)) {
                UserEntity iUser = (UserEntity) session.getAttribute(LOGIN_SESSION_KEY);
                if (iUser.getLogin().equals(user.getName())) {
                    invalidSession.add(session.getId());
                }
            }
        }
        //从内存中移除 并且使会话失效
        for (String invalid : invalidSession) {
            HttpSession httpSession = ALL_SESSION.get(invalid);
            ALL_SESSION.remove(invalid);
            httpSession.invalidate();
        }

        HttpServletRequest request = currentRequest();
        request.getSession().setAttribute(LOGIN_SESSION_KEY, user);
        request.getSession().setAttribute(LOGIN_USER_INFO_SESSION_KEY, attribute);
        ALL_SESSION.put(request.getSession().getId(), request.getSession());
    }

}
